﻿using System.Linq;
using Database.Context;
using QuanLyThongTinCoSo.BaseForm;
using QuanLyThongTinCoSo.Biz;
using QuanLyThongTinCoSo.Common;
using Model = Database.Models;

namespace QuanLyThongTinCoSo.Forms.PhieuDieuTraCoSo
{
    public partial class frmPhuongTienPCCC : frmDetail
    {
        public frmPhuongTienPCCC()
        {
            InitializeComponent();
            Text = @"Thêm Thẩm duyệt PCCC";
            drlNhomPhuongTien.Focus();
        }

        public frmPhuongTienPCCC(int id)
        {
            InitializeComponent();
            Text = @"Sửa Thẩm duyệt PCCC";
            drlNhomPhuongTien.Focus();
            _fromtype = Constant.TypeForm.Edit;
            _id = id;

            var biz = new PhuongTienCoSoBiz();
            var model = biz.GetItemById(id) as Model.PhuongTienCoSo;
            if (model == null) return;

            drlNhomPhuongTien.SelectedValue = model.PhanLoaiChiTietPhuongTiens.NhomPhuongTienCoSoes;
            txtTenPhuongTien.Text = model.Ten;
            drlPhanLoaiChiTiet.SelectedValue = model.PhanLoaiChiTietPhuongTiens;
            if (model.SoLuong != null) txtSoLuong.Value = (decimal) model.SoLuong;
        }

        private void frmPhuongTienPCCC_Load(object sender, System.EventArgs e)
        {
            LoadNhomPhuongTien();
        }

        private void LoadNhomPhuongTien()
        {
            var biz = new NhomPhuongTienPCCCCoSoBiz();
            drlNhomPhuongTien.DataSource = biz.GetAll();
            drlNhomPhuongTien.DisplayMember = "Ten";
        }

        private void LoadPhanLoaiChiTiet(int id)
        {
            var biz = new PhanLoaiChiTietPhuongTienPCCCCoSoBiz();
            drlPhanLoaiChiTiet.DataSource = biz.GetAll().Where(m=>m.NhomPhuongTienCoSoes.Id==id).ToList();
            drlPhanLoaiChiTiet.DisplayMember = "Ten";
        }

        private void drlNhomPhuongTien_SelectedValueChanged(object sender, System.EventArgs e)
        {
            var item = drlNhomPhuongTien.SelectedValue as Model.NhomPhuongTienPCCCCoSo;
            LoadPhanLoaiChiTiet(item.Id);
        }

        #region Validation

        protected override bool Validation()
        {
            bool flag = true;

            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtTenPhuongTien.Text))
            {
                errorProvider.SetError(txtTenPhuongTien, "Bạn chưa nhập Tên phương tiện. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            return flag;
        }

        #endregion

        #region Insert

        protected override void Insert()
        {
            var context = new Context();
            var model = new Model.PhuongTienCoSo();

            var phanLoaiChiTietBiz = new PhanLoaiChiTietPhuongTienBiz(context);
            var itemPhanLoaiChiTiet = drlPhanLoaiChiTiet.SelectedValue as Model.PhanLoaiChiTietPhuongTienPCCCCoSo;
            model.PhanLoaiChiTietPhuongTiens = (Model.PhanLoaiChiTietPhuongTienPCCCCoSo) phanLoaiChiTietBiz.GetItemById(itemPhanLoaiChiTiet.Id);
            model.Ten = txtTenPhuongTien.Text.Trim();
            model.SoLuong = (int?) txtSoLuong.Value;
            model.TinhTrangHoatDong_Tot = (int?) txtTTHD_Tot.Value;
            model.TinhTrangHoatDong_TrungBinh = (int?)txtTTHD_TrungBinh.Value;
            model.TinhTrangHoatDong_Kem = (int?)txtTTHD_Kem.Value;
            model.TinhTrangHoatDong_HuHong = (int?)txtTTHD_HuHong.Value;
            model.Vitri = txtVitri.Text.Trim();
            var biz = new PhuongTienCoSoBiz(context);
            biz.Insert(model);
        }

        #endregion

        #region Edit

        protected override void Edit()
        {
            var context = new Context();
            var biz = new PhuongTienCoSoBiz(context);
            var item = biz.GetItemById(_id) as Model.PhuongTienCoSo;
            if (item != null)
            {
                item.Ten = txtTenPhuongTien.Text.Trim();
                item.SoLuong = (int?)txtSoLuong.Value;
                item.TinhTrangHoatDong_Tot = (int?)txtTTHD_Tot.Value;
                item.TinhTrangHoatDong_TrungBinh = (int?)txtTTHD_TrungBinh.Value;
                item.TinhTrangHoatDong_Kem = (int?)txtTTHD_Kem.Value;
                item.TinhTrangHoatDong_HuHong = (int?)txtTTHD_HuHong.Value;
                item.Vitri = txtVitri.Text.Trim();

                var phanLoaiChiTietBiz = new PhanLoaiChiTietPhuongTienBiz(context);
                var itemPhanLoaiChiTiet = drlPhanLoaiChiTiet.SelectedValue as Model.PhanLoaiChiTietPhuongTienPCCCCoSo;
                item.PhanLoaiChiTietPhuongTiens = (Model.PhanLoaiChiTietPhuongTienPCCCCoSo) phanLoaiChiTietBiz.GetItemById(itemPhanLoaiChiTiet.Id);
                biz.Update(item);
            }
        }

        #endregion
    }
}